import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { FetchService } from '../../../core/net/fetch.service';
import { ActivatedRoute, Router } from '@angular/router';

@Component({
  selector: 'app-pm-history-version',
  templateUrl: './pm-history-version.component.html',
  styleUrls: ['./pm-history-version.component.scss']
})
export class PmHistoryVersionComponent implements OnInit {
    baseUrl = (window as any).environment.apiBaseUrl;
    pdfSrc = '';
    listData = [];
    selectedItemId;
    projectId;
    num;

    constructor(
        private message: NzMessageService,
        private fetchService: FetchService,
        private routerInfo: ActivatedRoute,
        private router: Router
    ) {
        this.projectId = this.routerInfo.snapshot.queryParams['projectId'];
        this.num = this.routerInfo.snapshot.queryParams['num'];
    }

    ngOnInit(): void {
        this.queryList();
    }

    queryList() {
        this.fetchService.post<any>('archives/wrsProjectFileVersion/list', {projectId: this.projectId, num: this.num})
            .subscribe(res => {
                this.listData = res;
                /* this.listData = [
                   {id: 1, version: '1.1', fileId: '390b7aeb92e84038a7f3e206c6db4695', createDate: '2020-03-18T00:00:00.000+0000'},
                   {id: 2, version: '1.2', fileId: '390b7aeb92e84038a7f3e206c6db4695', createDate: '2020-03-18T00:00:00.000+0000'},
                   {id: 3, version: '1.3', fileId: '390b7aeb92e84038a7f3e206c6db4695', createDate: '2020-03-18T00:00:00.000+0000'},
                   {id: 4, version: '1.4', fileId: '390b7aeb92e84038a7f3e206c6db4695', createDate: '2020-03-18T00:00:00.000+0000'},
                   {id: 5, version: '1.5', fileId: '390b7aeb92e84038a7f3e206c6db4695', createDate: '2020-03-18T00:00:00.000+0000'},
                 ];*/
                if (this.listData.length > 0) {
                    this.changePdf(this.listData[0]);
                }
            });
    }

    changePdf(item): void {
        this.selectedItemId = item.id;
        this.pdfSrc = `${(window as any).environment.apiBaseUrl}comm/file/view/${item.fileId}`;
    }

    downloadPdf(): void {
        $('#download').trigger('click');
    }

    back(): void {
        this.router.navigate(['/pm/version-mng']);
    }

    onDelete(id, projectFileId): void {
        this.fetchService.post(`archives/wrsProjectFileVersion/delete`, {
            id,
            projectId: projectFileId
        }).subscribe(resp => {
            this.message.info('删除成功');
            this.queryList();
        });
    }
}
